package com.ygs.secs.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ygs.secs.model.AuthInfomation;
import com.ygs.secs.model.CharacteristicActivities;
import com.ygs.secs.model.Enterprise;
import com.ygs.secs.model.EnterprisePhoto;
import com.ygs.secs.service.AuthInfomationService;
import com.ygs.secs.service.CharacteristicActivitiesService;
import com.ygs.secs.service.EnterprisePhotoService;
import com.ygs.secs.service.EnterpriseService;
import com.ygs.secs.tools.CustomTools;
import com.ygs.secs.tools.Pinyin4jUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.awt.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 企业管理
 *
 * @author Yunxi
 */
@RequestMapping("/ygssecs")
@Controller
public class EnterpriseManageController {
    //上传图片真实路径
    @Value("${uppath}")
    String uploadPath;
    //映射路径
    @Value("${mappingpath}")
    String mappath;

    @Autowired
    EnterpriseService enterpriseService;
    @Autowired
    AuthInfomationService authService;
    @Autowired
    CharacteristicActivitiesService activitiesService;
    @Autowired
    EnterprisePhotoService photoService;

    /**
     * 企业登录
     *
     * @param enterpriseid 企业ID
     * @param password     MD5加密后的密码串
     * @return 通用json返回格式
     */
    @RequestMapping("/enterpriseLogin")
    @ResponseBody
    public Map<String, Object> enterpriseLogin(String enterpriseid, String password) {
        Map<String, Object> map = new HashMap<>();
        try {
            Enterprise enterprise = enterpriseService.selectByPrimaryKey(enterpriseid);
            if (enterprise != null) {
                if (enterprise.getPassword().equals(password)) {
                    map.put("code", 200);
                    map.put("msg", "登录成功");
                    map.put("data", enterprise);
                } else {
                    map.put("code", 201);
                    map.put("msg", "登陆失败,密码错误，请重新输入");
                }
            } else {
                map.put("code", 202);
                map.put("msg", "登陆失败,账号不存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", 500);
            map.put("msg", "system error");
        }
        return map;
    }

    /**
     * 注册
     *
     * @param enterprisename 企业名称
     * @param password       密码
     * @param emailaccount   邮箱
     * @return 注册结果
     */

    @RequestMapping("/registration")
    @ResponseBody
    public Map<String, Object> enterpriseRegistration(@RequestParam("enterprisename") String enterprisename, @RequestParam("password") String password, @RequestParam(required = false) String emailaccount) {
        Map<String, Object> map = new HashMap<>();
        String nameofpinyin = Pinyin4jUtil.getFirstSpellPinYin(enterprisename, false);
        String enterpiseid = nameofpinyin + CustomTools.getRandom4(5);
        Enterprise enterprise = new Enterprise();
        enterprise.setEnterpriseid(enterpiseid);
        enterprise.setEnterprisename(enterprisename);
        enterprise.setPassword(password);
        enterprise.setLogoimg("1.jpg");
        if (emailaccount != null) {
            enterprise.setEmailaccount(emailaccount);
        }
        int i = enterpriseService.insertSelective(enterprise);
        if (i > 0) {
            map.put("code", 200);
            map.put("msg", "注册成功");
            map.put("data", enterprise);
        } else {
            map.put("code", 500);
            map.put("msg", "注册成功");
            map.put("data", null);
        }
        return map;
    }

    /**
     * 获取企业工商认证信息
     *
     * @param enterpriseid 企业ID
     * @return json通用格式
     */
    @RequestMapping("/getenterinfo")
    @ResponseBody
    public Map<String, Object> getenterinfo(String enterpriseid) {
        Map<String, Object> map = new HashMap<>();
        try {
            AuthInfomation authInfomation = authService.getauthinfo(enterpriseid);
            if (authInfomation != null) {
                map.put("code", 200);
                map.put("msg", "数据请求成功");
                map.put("data", authInfomation);
            } else {
                map.put("code", 201);
                map.put("msg", "你还未填写工商信息进行认证");
                map.put("data", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", 500);
            map.put("msg", "数据请求失败");
            map.put("data", null);
        }
        return map;
    }

    /**
     * 更改密码
     *
     * @param priseid 企业ID
     * @param oldpwd  旧密码
     * @param newpwd  新密码
     * @return json通用格式
     */
    @RequestMapping("/changepwd")
    @ResponseBody
    public Map<String, Object> changepwd(String priseid, String oldpwd, String newpwd) {
        System.out.println(oldpwd);
        System.out.println(newpwd);
        Map<String, Object> map = new HashMap<>();
        Enterprise enterprise = enterpriseService.selectByPrimaryKey(priseid);
        if (enterprise.getPassword().equals(oldpwd)) {
            enterprise.setPassword(newpwd);
            try {
                int i = enterpriseService.updatepwd(enterprise);
                if (i > 0) {
                    map.put("code", 200);
                    map.put("msg", "密码修改成功");
                }
            } catch (Exception e) {
                e.printStackTrace();
                map.put("code", 500);
                map.put("msg", "系统错误");
            }
        } else {
            map.put("code", 201);
            map.put("msg", "原密码错误");
        }
        return map;
    }

    /**
     * 添加/更新认证信息
     *
     * @param authinfo 认证信息json
     * @return 通用json返回格式
     */
    @RequestMapping("/updataauthinfo")
    @ResponseBody
    public Map<String, Object> updataauthinfo(String authinfo) {
        AuthInfomation authInfomation = JSONObject.parseObject(authinfo, AuthInfomation.class);
        Map<String, Object> map = authService.odateauthinfo(authInfomation);
        return map;
    }

    /**
     * 查询企业认证状态
     *
     * @param priseid 企业ID
     * @return 通用json返回格式
     */
    @RequestMapping("/getenterByid")
    @ResponseBody
    public Map<String, Object> getenterauthstatus(String priseid) {
        Map<String, Object> map = new HashMap<>();
        try {
            Enterprise enterprise = enterpriseService.selectByPrimaryKey(priseid);
            if (enterprise != null) {
                map.put("code", 200);
                map.put("msg", "数据查询成功");
                map.put("data", enterprise);
            } else {
                map.put("code", 201);
                map.put("msg", "数据查询失败，没有此企业");
                map.put("data", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", 500);
            map.put("msg", "system error ");
        }
        return map;
    }

    /**
     * 添加特色活动
     *
     * @param activityjson 活动json
     * @return 通用json但会格式
     */
    @RequestMapping("/addFeaturedActivities")
    @ResponseBody
    public Map<String, Object> addFeaturedActivities(String activityjson) {
        Map<String, Object> map = new HashMap<>();
        try {
            CharacteristicActivities activity = JSONObject.parseObject(activityjson, CharacteristicActivities.class);
            int i = activitiesService.insertSelective(activity);
            if (i > 0) {
                map.put("code", 200);
                map.put("msg", "添加成功");
            } else {
                map.put("code", 500);
                map.put("msg", "添加失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", 500);
            map.put("msg", "添加失败");
        }
        return map;
    }

    /**
     * 添加图片集
     *
     * @param photoCollection 图片地址数组
     * @param enterjson       企业json
     * @return 通用json返回格式
     */
    @RequestMapping("/addPhotoCollection")
    @ResponseBody
    public Map<String, Object> addPhotoCollection(String photoCollection, String enterjson) {
        Map<String, Object> map = new HashMap<>();
        try {
            Enterprise enterprise = JSONObject.parseObject(enterjson, Enterprise.class);
            List<EnterprisePhoto> enterprisePhotos = new ArrayList<>();
            List<String> imgs = JSONArray.parseArray(photoCollection, String.class);
            for (String img : imgs) {
                EnterprisePhoto enterprisePhoto = new EnterprisePhoto();
                enterprisePhoto.setEnterprise(enterprise);
                enterprisePhoto.setPhotoname(img);
                enterprisePhotos.add(enterprisePhoto);
            }
            int i = photoService.batchInsert(enterprisePhotos);
            if (i == enterprisePhotos.size()) {
                map.put("code", 200);
                map.put("msg", "添加成功");
            } else {
                map.put("code", 500);
                map.put("msg", "添加失败");
            }
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "system error");
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 查询上传图片
     *
     * @param enterpriseid 企业ID
     * @return 通用json返回格式
     */
    @RequestMapping("/getEnterpriseImg")
    @ResponseBody
    public Map<String, Object> getEnterpriseImg(String enterpriseid) {
        Map<String, Object> map = new HashMap<>();
        try {
            List<EnterprisePhoto> enterprisePhoto = photoService.selectEnterpriseImg(enterpriseid);
            map.put("code", 200);
            map.put("msg", "数据请求成功");
            map.put("data", enterprisePhoto);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", 500);
            map.put("msg", "数据请求失败");
            map.put("data", null);
        }
        return map;
    }

    /**
     * 删除上传图片
     *
     * @param photoid 图片ID
     * @return 通用json返回格式
     */
    @RequestMapping("/deleteEnterpriseImg")
    @ResponseBody
    public Map<String, Object> deleteEnterpriseImg(Integer photoid) {
        Map<String, Object> map = new HashMap<>();
        try {
            EnterprisePhoto enterprisePhoto = photoService.selectByPrimaryKey(photoid);
            if (CustomTools.deliamge(enterprisePhoto.getPhotoname(), mappath, uploadPath)) {
                int delcnt = photoService.deleteByPrimaryKey(photoid);
                if (delcnt > 0) {
                    map.put("code", 200);
                    map.put("msg", "删除成功");
                } else {
                    map.put("code", 300);
                    map.put("msg", "删除失败");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", 500);
            map.put("msg", "数据请求失败");
        }
        return map;
    }


    /**
     * 查询企业活动
     *
     * @param enterpriseid 企业ID
     * @return 通用json返回格式
     */
    @RequestMapping("/getEnterpriseAct")
    @ResponseBody
    public Map<String, Object> getEnterpriseAct(String enterpriseid) {
        Map<String, Object> map = new HashMap<>();
        try {
            List<CharacteristicActivities> activities = activitiesService.selectByEnterpriseID(enterpriseid);
            map.put("code", 200);
            map.put("msg", "数据请求成功");
            map.put("data", activities);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", 500);
            map.put("msg", "数据请求失败");
            map.put("data", null);
        }
        return map;
    }


    /**
     * 删除企业活动
     *
     * @param activitieid 活动ID
     * @return 通用json返回格式
     */
    @RequestMapping("/deleteEnterpriseAct")
    @ResponseBody
    public Map<String, Object> deleteEnterpriseAct(Integer activitieid) {
        Map<String, Object> map = new HashMap<>();
        try {
            CharacteristicActivities activities = activitiesService.selectByPrimaryKey(activitieid);
            if (CustomTools.deliamge(activities.getActivitieimage(), mappath, uploadPath)) {
                int delcnt = activitiesService.deleteByPrimaryKey(activitieid);
                if (delcnt > 0) {
                    map.put("code", 200);
                    map.put("msg", "删除成功");
                } else {
                    map.put("code", 300);
                    map.put("msg", "删除失败");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", 500);
            map.put("msg", "数据请求失败");
        }
        return map;
    }

}
